package cn.tangsihai.org.service.impl;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.service.impl.BasicServiceImpl;
import cn.tangsihai.basic.util.MD5Utils;
import cn.tangsihai.basic.util.PageList;
import cn.tangsihai.basic.util.StrUtils;
import cn.tangsihai.org.domain.Employee;
import cn.tangsihai.org.domain.Shop;
import cn.tangsihai.org.mapper.EmployeeMapper;
import cn.tangsihai.org.mapper.ShopMapper;
import cn.tangsihai.org.query.ShopQuery;
import cn.tangsihai.org.service.IShopService;
import cn.tangsihai.user.domain.Logininfo;
import cn.tangsihai.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) // 事务
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    @Transactional
    public void shopRegister(Shop shop) {
        Employee admin = shop.getAdmin();
        // 检验密码一致性
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new MyException("密码不一致！");
        }
        // 判断用户名是否已经存在
        Employee employee = employeeMapper.findByUsername(admin.getUsername());
        if (employee != null){
            throw new MyException("用户已存在");
        }
        // 两张表先添加employee，获取自增长主键
        Employee employee1 = initEmployee(admin);
        employeeMapper.add(employee1); // 已经获取到了自增长主键id
        // 把employee的自增长id设置到shop中添加shop
        shop.setAdmin(employee1);
        shop.setAdmin_id(employee1.getId());
        shopMapper.add(shop);
        // 更新employee表中的shop_id字段
        employee1.setShop_id(shop.getId());

        // 更新logininfo，返回自增长id
        // 通过employee准备数据
        Logininfo logininfo = init(employee1);
        logininfoMapper.add(logininfo);
        // 把logininfo的自增长id设置到employee中添加logininfo_id字段
        employee1.setLogininfo(logininfo);
        employee1.setLogininfo_id(logininfo.getId());
        employeeMapper.update(employee1); // 更新shop表中的shop_id
    }

    // 准备数据
    private Employee initEmployee(Employee admin) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(admin, employee);
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        employee.setPassword(MD5Utils.encrypByMd5(admin.getPassword() + salt)); // 加密加盐
        employee.setState(1);
        return employee;
    }

    private Logininfo init(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0); // 添加店铺，type=0，工作人员，1普通用户
        return logininfo;
    }
}
